my=function(){}
my.getRandomNum=function () {
    return new Date().getTime() + Math.random() * 1000000000 + "" + Math.random() * 1000000000;
}
my.createXHR = function(req) {
    var xhr = false;
    if (window.XMLHttpRequest) {
        //在IE7+，firefox，Mozilla浏览器中被支持
        xhr = new XMLHttpRequest();
    } else {
        //在IE5.5,IE6中被支持
        if (window.ActiveXObject) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    if (xhr) {
        var beginTime=new Date().getTime;
        //如果是异步则注册回调函数
            xhr.onreadystatechange = function() {
                 if(req.timeout>0){
                          if(new Date().getTime-beginTime>=req.timeout){
                                req.error(xhr, xhr.status,"对不起，链接超时");
                          }
                    }
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        req.success(xhr, xhr.responseText, xhr.status);
                    } else {
                        req.error(xhr, xhr.status);
                    }
                }
            };
       if(!req.async) {//若是同步，则周期性检测
        var xhrmonitor=window.setInterval(function(){
             if(req.timeout>0){
                          if(new Date().getTime-beginTime>=req.timeout){
                                req.error(xhr, xhr.status,"对不起，链接超时");
                                window.clearInterval(xhrmonitor);
                          }
                    }
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        req.success(xhr, xhr.responseText, xhr.status);
                    } else {
                        req.error(xhr, xhr.status);
                    }
                     window.clearInterval(xhrmonitor);
                }
        },50);
        }
       /* var poststr="";
        for(var key in req.data){
             if(poststr==""){
                 poststr=key+"="+encodeURI(req.data[key]);
             }else{
                  poststr+="&"+key+"="+encodeURI(req.data[key]);
             }
        }*/
        xhr.open(req.type,req.url,req.async,"","");
        if(req.redirecttype==0){
            xhr.setRequestHeader("Ajax_Redirect_Type",0);
        }
        // Set header so the called script knows that it's an XMLHttpRequest
        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        // Set the Accepts header for the server, depending on the dataType
        if(req.accepts){
        xhr.setRequestHeader("Accept", req.dataType && req.accepts[ req.dataType ] ?req.accepts[ req.dataType ] + ", */*" :req.accepts._default );
        }
        xhr.send(req.data);
    } else {
        alert("浏览器不支持AJAX，请升级");
    }
//回调函数
 function method() {
    if (xhr.readyState == 4) {
       if (xhr.status == 200) {
       //这里就可以利用XMLHttpRequest对象提供的方法进行操作
     }
  }
}
}
my.ajax=function (theurl,thecallback,isAsync,thetype,thedata,redircttype,thetarget) {
        if(theurl.indexOf("?")==-1){
            theurl+="?"+my.getRandomNum();
        }else{
            theurl+="&"+my.getRandomNum();
        }
        if(thetarget==undefined||thetarget==null||thetarget=="") thetarget="_self";
        my.createXHR({
            url: theurl,
            type: thetype,
            dataType: 'XML',
            async:isAsync,
            timeout: 50000,
            data:thedata,
            redirecttype:redircttype,
            error: function(xhr, status,txt){
                if(!txt) txt="";
                thecallback("ajax_EXCEPTION"+txt);
            },
            success:function(xhr,data,status){
                if (redircttype == "0") {//自动处理重定向
                    var target = xhr.getResponseHeader("Ajax_Response_Redirect");
                    if (target != undefined && target != null && target != "") {//说明是forward或redirect
                         window.open(target,thetarget);
                    }else{
                        if(my.isHtmlDoc(data)){//返回的是一个完整的html文档
                            var qindex=theurl.indexOf("?")
                            if(qindex>-1) target=theurl.substring(0,qindex);
                            else target=theurl;
                            my.submitWithHiddenForm(target,"Ajax_Response_Forward",data,thetarget);
                        }else{
                            var appendJSIndex=data.indexOf("<!--!!!!!MyActionJS_COMM######-->");
                            if(appendJSIndex>-1){
                                data=data.substring(0,appendJSIndex)
                            }
                            thecallback(data);
                        }
                    }
                }else{//手工处理重定向
                    thecallback(data);
                }
            }
        });

    }
//获取函数的函数名
my.getFunctionName=function (call){
  if(call==undefined||call==null) return "";
  var func=call+"";
  func=func.substring(0,func.indexOf("("));
  func=func.substring("function".length);
  func=my.trim(func);
  return func;
}
//去前后空格函数
my.trim=function (str)
{
    return (str || "").replace( /^\s+|\s+$/g, "" );
}
//去所有空格函数
my.trimAll=function (str)
{
    return (str || "").replace( /\s/ig, "" );
}
//设置表单中组件的回车事件
my.setEnterKeyEvent=function (id){
    get(id).onkeydown=function(event){
        if(event==undefined){
            event=window.event;
        }
        if(event.keyCode==13){
            this.form.submit();
        }
    }
}


my.submitFormAjax=function (form,callback){
     var method=form.method;
     var elems=form.elements;
     var target=form.target;
     var params=form.className.split("_");
     var isAsync=params[2]==1;
     var redirectType=params[3];
     if(method.toLowerCase()=="get"){
          var params="";
          for(var i=0;i<elems.length;i++){
              var elm=elems[i];
              if(elm.tagName.toLowerCase()!="textarea"){
                  if(elm.type.toLowerCase()=="button"){
                      continue;
                  } if(elm.type.toLowerCase()=="submit"){
                      continue;
                  }else if(elm.type.toLowerCase()=="checkbox"&&elm.checked==false){
                      continue;
                  }else if(elm.type.toLowerCase()=="radio"&&elm.checked==false){
                      continue;
                  }
                  params+=elm.name+"="+encodeURI(elm.value)+"&";

              }else{
                  params+=elm.name+"="+encodeURI(elm.innerHTML)+"&";
              }
          }
          if(params!="") params=params.substring(0,params.length-1);
          var url=form.action;
          if(url.indexOf("?")>-1) url+="&"+params;
          else url+="?"+params;
          my.ajax(url,callback,isAsync,"GET",null,redirectType,target);
     }else if(method.toLowerCase()=="post"){
           var params="";
          for(var i=0;i<elems.length;i++){
              var elm=elems[i];
              if(elm.tagName.toLowerCase()!="textarea"){
                  if(elm.type.toLowerCase()=="button"){
                      continue;
                  }if(elm.type.toLowerCase()=="submit"){
                      continue;
                  }else if(elm.type.toLowerCase()=="checkbox"&&elm.checked==false){
                      continue;
                  }else if(elm.type.toLowerCase()=="radio"&&elm.checked==false){
                      continue;
                  }
                  params+=elm.name+"="+encodeURI(elm.value)+"&";
              }else{
                 params+=elm.name+"="+encodeURI(elm.innerHTML)+"&";
              }
          }
          if(params!="") params=params.substring(0,params.length-1);
          var url=form.action;
         /*for(var key in data){
             alert(key+":"+data[key])
         }*/
         my.ajax(url,callback,isAsync,"POST",params,redirectType,target);
     }
}


my.addloadevent=function (func) {
    var oldonload = window.onload;
    if (typeof window.onload != "function") {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

my.addSubmitEvent=function (form,func) {
    var oldsubmit = form.onsubmit;
    if (typeof oldsubmit != "function") {
        form.onsubmit =func;
    } else {
        form.onsubmit = function() {
           if(oldsubmit()){
               return func();
           }
           return false;
        }
    }
}
my.checkAjaxForms=function (){
    var forms=document.getElementsByTagName("FORM");
    if(forms==null) return;
    for(var i=0;i<forms.length;i++){
        try{
        var form=forms[i];
        if(form.className=="") form.className="ajax_form_0_0";
        if(form.className.indexOf("ajax_form_")==-1) continue;
        if(form.className=="ajax_form_not") continue;
        if(form.className&&form.className.indexOf("ajax_form_")>-1){
             my.addSubmitEvent(form,function(){
                var evt=my.getEvent();
                var form = evt.srcElement ? evt.srcElement : evt.target
                 //以ajax的方式提交数据
                 var errorFuncName="ajax_form_"+form.id+"_onError";
                  var successFuncName="ajax_form_"+form.id+"_onSuccess";
                 my.submitFormAjax(form,function(data){
                     if(data.indexOf("ajax_EXCEPTION")==0){//错误处理
                          if(data!="ajax_EXCEPTION"){
                              data=data.substring("ajax_EXCEPTION".length)
                          }else{
                              data="对不起，发生了不可预知的异常，请检查您的输入或联系管理员";
                          }

                          if(window[errorFuncName]){//若定义了错误处理函数
                              window[errorFuncName](data);
                          }else{
                               my.disableAndInfo(data,true);
                          }
                     }else{//成功处理
                          if(window[successFuncName]){//若定义了成功处理函数
                              window[successFuncName](data);
                          }else{
                              my.disableAndInfo(data,true);
                          }
                     }
                 });
                 return false;
             });
        }}catch(e){}
    }
}
//input验证框架
my.addInputFocusEvent=function(input,func){
   var oldfocus = input.onfocus;
    if (typeof oldfocus != "function") {
        input.onfocus = func;
    } else {
        input.onfocus = function() {
           func();
           oldfocus();
        }
    }
}

my.addInputBlurEvent=function(input,func){
   var oldblur = input.onblur;
    if (typeof oldblur != "function") {
        input.onblur = func;
    } else {
        input.onblur = function() {
            func();
            oldblur();
        }
    }
}
my.addInputMouseOverEvent=function(input,func){
   var oldover = input.onmouseover;
    if (typeof oldover != "function") {
        input.onmouseover = func;
    } else {
        input.onmouseover = function() {
            func();
            oldover();
        }
    }
}
my.addInputMouseOutEvent=function(input,func){
   var oldout = input.onmouseout;
    if (typeof oldout != "function") {
        input.onmouseout = func;
    } else {
        input.onmouseout = function() {
            func();
            oldout();
        }
    }
}
//获取组件的左上角坐标
my.getIE=function(e){
         var t=e.offsetTop;
         var l=e.offsetLeft;
         while(e=e.offsetParent){
                t+=e.offsetTop;
                l+=e.offsetLeft;
            }
         return [t,l];
}
my.showValidationInfo=function(evt,errMsg){
       var obj=my.getEventSource();
       if(obj.id=="") return;
       if(obj.getAttribute("ValidationInfo")==undefined||obj.getAttribute("ValidationInfo")==null){
           obj.setAttribute("ValidationInfo","");
       }
       var infoArr=[];
        infoArr[0]="";
        infoArr[1]="";
        if(document.getElementById(obj.id+"_reg")!=null) infoArr[0]=document.getElementById(obj.id+"_reg").innerHTML;
        if(document.getElementById(obj.id+"_info")!=null) infoArr[1]=document.getElementById(obj.id+"_info").innerHTML;
       var info="";
       var infoDiv=null;
       var infoId=obj.id+"_ValidationInfoDIV";
       infoDiv=document.getElementById(infoId);
       if(infoDiv==null){
           infoDiv=document.createElement("div");
           infoDiv.id=infoId;
           infoDiv.style.disable="none";
           infoDiv.style.position="absolute";
           infoDiv.style.border='1px solid blue';
           document.body.appendChild(infoDiv);
       }
       var pos=my.getIE(obj);
       infoDiv.style.left=pos[1]+obj.offsetWidth;
       infoDiv.style.top =pos[0]-obj.offsetHeight;
       if(evt==0){//获得焦点
                  info=infoArr[1];
                  infoDiv.style.display="block";
       }else if(evt==1){//失去焦点
                  var validationRt=false;
                  if(infoArr[0]==""){//验证成功
                      validationRt=true;
                      obj.setAttribute("ValidationInfo","输入成功");
                  }else{
                      var re = new RegExp(infoArr[0]);
                      if(re.test(obj.value)){//验证成功
                           validationRt=true;
                           obj.setAttribute("ValidationInfo","输入成功");
                      }else{//验证失败
                          obj.form.setAttribute("INPUT_VALIDATION",0);
                          validationRt=false;
                          obj.setAttribute("ValidationInfo","输入失败");
                      }
                  }
                if(validationRt){
                    obj.style.backgroundColor="darkseagreen";
                }else{
                    obj.style.backgroundColor="red";
                }
                infoDiv.style.display="none";
       }else if(evt==2){//鼠标over
                  info=infoArr[1];
                  infoDiv.style.display="block";

       }else if(evt==3){//鼠标out
                 //如果当前的input没有焦点则隐藏
                 if(document.activeElement.id!=obj.id){
                     infoDiv.style.display="none";
                 }else{
                     infoDiv.style.display="block";
                 }
       }else if(evt=100){//手工提示
                obj.form.setAttribute("INPUT_VALIDATION",0);
                obj.style.backgroundColor="red";
                info=infoArr[1];
                if(errMsg!=undefined&&errMsg!=null){
                    obj.setAttribute("ValidationInfo","输入失败【"+errMsg+"】");
                }
       }

       var newtitle="";
       if(info==""){
           newtitle=obj.getAttribute("ValidationInfo");
       }else{
           if(obj.getAttribute("ValidationInfo")==""){
               newtitle=info;
           }else{
               newtitle=info+"<br>"+obj.getAttribute("ValidationInfo");
           }
       }
       if(newtitle==""){
           infoDiv.style.display="none";
       }else{
          infoDiv.innerHTML=newtitle;  
       }

}
my.initInputsValidations=function(){
    var inputs=document.getElementsByTagName("INPUT");
    if(inputs==null) return;
    for(var i=0;i<inputs.length;i++){
        var input=inputs[i];
        if(input.type.toUpperCase()=="TEXT"||input.type.toUpperCase()=="PASSWORD"){
            my.addInputFocusEvent(input,function(){
                my.showValidationInfo(0);
            });
            my.addInputBlurEvent(input,function(){
                my.showValidationInfo(1);
            });
             my.addInputMouseOverEvent(input,function(){
                my.showValidationInfo(2);
            });
             my.addInputMouseOutEvent(input,function(){
                my.showValidationInfo(3);
            });
        }
    }
    //为各form添加有关验证的submit事件
    var forms=document.getElementsByTagName("FORM");
    if(forms==null) return;
    for(var i=0;i<forms.length;i++){
        var form=forms[i];
        my.addSubmitEvent(form,function(){
            form.setAttribute("INPUT_VALIDATION",1);
            var inps=form.elements;
            if(inps==null) return;
            for(var j=0;j<inps.length;j++){
                var inp=inps[j];
                if(inp.type.toUpperCase()=="TEXT"||inp.type.toUpperCase()=="PASSWORD"){
                     if(my.getBrowserType()=="fox"){
                         try{
                             my.eventSource=inp;
                             inp.onblur();
                             my.eventSource=null;
                         }catch(e){
                             my.eventSource=null;
                         }finally{
                              my.eventSource=null;
                         }
                     }else{
                         inp.fireEvent("onblur");
                     }
                     //my.showValidationInfo(1,null,inp.id);
                }
            }
            return form.getAttribute("INPUT_VALIDATION")==1;
        });
    }
}
//input验证框架 完
//获取事件对象
my.getEvent=function () {
    var ev = window.event;
    if (!ev) {
        var c = this.getEvent.caller;
        while (c) {
            ev = c.arguments[0];
            if (ev && Event == ev.constructor)
                break;
            c = c.caller;
        }
    }
    return ev;
}

my.eventSource=null;//用于保存this对象，尤其是在firefox模拟fireEvent时有用
//获取事件源
my.getEventSource=function(){
    if(my.getBrowserType()=="fox"){
         if(my.eventSource!=null) return  my.eventSource;
         var evt=my.getEvent();
         return evt.srcElement || evt.target;
    }else{
         var evt=my.getEvent();
         return evt.srcElement || evt.target;
    }

}

//构建隐藏form并提交数据
my.submitWithHiddenForm=function (action,name,value,target){
    var form=document.createElement("form");
    form.action=action;
    form.target=target;
    form.method="get";
    var input=document.createElement("input");
    input.type="hidden";
    input.name=name;
    input.value=value;
    form.appendChild(input);
    document.body.appendChild(form);
    form.submit();
}


my.getBrowserType=function (){//获取浏览器类型
    var browser = navigator.appName;
    var b_version = navigator.appVersion;
    var version = parseFloat(b_version);
    //alert(browser);
    if ((browser == "Netscape"))
    {
        return "fox";
    }
    else if (browser == "Microsoft Internet Explorer")
    {
        if (version >= 4)
        {
            return "ie4+";
        }
        else
        {
            return "ie4-";
        }
    }
    else
    {
        return "NSupport";
    }
}


//将可视组件移到鼠标事件的位置
my.moveComponentToMouse=function (obj){
     var evt=my.getEvent();
    if (my.getBrowserType() == "fox")
    {
        obj.style.left = evt.pageX + 10;
        obj.style.top = evt.pageY + 10;
    }
    else {
        obj.style.left = evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) + 10;
        obj.style.top = evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + 10;
    }
}


my.isHtmlDoc=function (str){//判断给定字符串是否为html文档
    if(str==null||str=="") return false;
    var newstr=str.toLowerCase();
    newstr=my.trimAll(newstr);
    if(newstr.indexOf("<html")>-1&&newstr.indexOf("</html>")>-1&&newstr.indexOf("<body")>-1&&newstr.indexOf("</body>")>-1){
        return true;
    }
    return false;
}


/*
屏蔽层
 */
my.disableAndInfo=function (str, closable) {
    var infodiv = document.getElementById("msgDiv");
    if (infodiv == null) {
        var msgw,msgh,bordercolor,titleheight,bordercolor,titlecolor;
        msgw = 500;//提示窗口的宽度
        msgh = 200;//提示窗口的高度
        titleheight = 25   //提示窗口标题高度
        bordercolor = "#336699";//提示窗口的边框颜色
        titlecolor = "#99CCFF";//提示窗口的标题颜色

        var msgObj = document.createElement("div")//创建一个div对象（提示框层）
        //定义div属性，即相当于
        // <div   id="msgDiv"   align="center"   style="background-color:white;   border:1px   solid   #336699;   position:absolute;   left:50%;   top:50%;   font:12px/1.6em   Verdana,Geneva,Arial,Helvetica,sans-serif;   margin-left:-225px;   margin-top:npx;   width:400px;   height:100px;   text-align:center;   line-height:25px;   z-index:100001;"> </div>
        msgObj.setAttribute("id", "msgDiv");
        msgObj.setAttribute("align", "center");
        msgObj.style.background = "white";
        msgObj.style.border = "1px   solid  " + bordercolor;
        msgObj.style.position = "absolute";
        msgObj.style.left = "50%";
        msgObj.style.top = "50%";
        msgObj.style.font = "12px/1.6em   Verdana,   Geneva,   Arial,   Helvetica,   sans-serif";
        msgObj.style.marginLeft = "-225px";
        msgObj.style.marginTop = -75 + document.documentElement.scrollTop + "px";
        msgObj.style.width = msgw + "px";
        msgObj.style.height = msgh + "px";
        msgObj.style.textAlign = "center";
        msgObj.style.lineHeight = "25px";
        msgObj.style.zIndex = "10001";
        var title = document.createElement("h4");//创建一个h4对象（提示框标题栏）
        //定义h4的属性，即相当于
        // <h4   id="msgTitle"   align="right"   style="margin:0;   padding:3px;   background-color:#336699;   filter:progid:DXImageTransform.Microsoft.Alpha(startX=20,   startY=20,   finishX=100,   finishY=100,style=1,opacity=75,finishOpacity=100);   opacity:0.75;   border:1px   solid   #336699;   height:18px;   font:12px   Verdana,Geneva,Arial,Helvetica,sans-serif;   color:white;   cursor:pointer;"   onclick=""> 关闭 </h4>
        title.setAttribute("id", "msgTitle");
        title.setAttribute("align", "left");
        title.style.margin = "0";
        title.style.padding = "3px";
        title.style.background = bordercolor;
        title.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=20,   startY=20,   finishX=100,   finishY=100,style=1,opacity=75,finishOpacity=100);";
        title.style.opacity = "0.75";
        title.style.border = "1px   solid  " + bordercolor;
        title.style.height = "18px";
        /*title.style.font="12px   Verdana,   Geneva,   Arial,   Helvetica,   sans-serif";
         title.style.color="white";
         title.style.cursor="pointer";*/



        document.body.appendChild(msgObj);//在body内添加提示框div对象msgObj
        document.getElementById("msgDiv").appendChild(title);//在提示框div中添加标题栏对象title

        var txt = document.createElement("p");//创建一个p对象（提示框提示信息）
        //定义p的属性，即相当于
        // <p   style="margin:1em   0;"   id="msgTxt"> 测试效果 </p>
        txt.style.margin = "1em   0"
        txt.setAttribute("id", "msgTxt");
        document.getElementById("msgDiv").appendChild(txt);//在提示框div中添加提示信息对象txt


    }
    var title=document.getElementById("msgTitle");
    var txt=document.getElementById("msgTxt");
    txt.innerHTML=str;
     if (closable) {
            title.innerHTML = "<table width='100%'><tr><td align='left'><font style='font:12px   Verdana,   Geneva,   Arial,   Helvetica,   sans-serif;color:white'>温馨提示</font></td><td align='right'><font style='font:12px   Verdana,   Geneva,   Arial,   Helvetica,   sans-serif;color:white;cursor:pointer' onclick='my.hideDisableAndInfo()'>关闭</font></td></tr></table>";
     }else{
            title.innerHTML = "<table><tr><td align='left'>温馨提示</td></tr></table>";
     }
    infodiv = document.getElementById("msgDiv");
    my.showDisableBgDiv();
    infodiv.style.display = "block";
}

my.showDisableBgDiv=function () {
    var bgdiv = document.getElementById("bgDiv");

    if (bgdiv == null) {
        var sWidth,sHeight;
        sWidth = document.body.offsetWidth;//浏览器工作区域内页面宽度
        sHeight = screen.height;//屏幕高度（垂直分辨率）
        //背景层（大小与窗口有效区域相同，即当弹出对话框时，背景显示为放射状透明灰色）
        var bgObj = document.createElement("div");//创建一个div对象（背景层）

        //定义div属性，即相当于
        // <div   id="bgDiv"   style="position:absolute;   top:0;   background-color:#777;   filter:progid:DXImagesTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75);   opacity:0.6;   left:0;   width:918px;   height:768px;   z-index:10000;"> </div>
        bgObj.setAttribute("id", "bgDiv");
        bgObj.style.position = "absolute";
        bgObj.style.top = "0";
        bgObj.style.background = "#777";
        bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
        bgObj.style.opacity = "0.6";
        bgObj.style.left = "0";
        bgObj.style.width = sWidth + "px";
        bgObj.style.height = sHeight + "px";
        bgObj.style.zIndex = "10000";
        document.body.appendChild(bgObj);//在body内添加该div对象
    }
    bgdiv = document.getElementById("bgDiv");
    bgdiv.style.display = "block";
}
my.hideDisableAndInfo=function () {
    var infodiv = document.getElementById("msgDiv");
    if (infodiv != null) {
        infodiv.style.display = "none";
    }
    my.hideDisableBgDiv();
}
my.hideDisableBgDiv=function () {
    var bgdiv = document.getElementById("bgDiv");
    if (bgdiv != null) {
        bgdiv.style.display = "none";
    }
}
/*
屏蔽层结束
* */
my.initInputsValidations();
my.checkAjaxForms();
